package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.goods.pojo.Category;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("")
    public Result findAll() {
//    return categoryService.findAll();
        List<Category> list = categoryService.list();
        return new Result(true, StatusCode.OK, null, list);
    }

    @PostMapping("")
    public Result addCategory(@RequestBody Category category) {
        return categoryService.addCategory(category);
    }
    @GetMapping("/search/{page}/{size}")
    public Result findPage( @PathVariable("page") Integer page,
                            @PathVariable("size") Integer size){
        IPage<Category> page1 = new Page<>(page, size);
        LambdaQueryWrapper<Category> categoryWrapper = Wrappers.lambdaQuery(Category.class);
        categoryService.page(page1,categoryWrapper);
        //PageBean<Category> pageBean = new ConverterUtil<Category>().convertFromPageInfoToPageBean(page1);
        PageResult<Category> categoryPageResult = new PageResult<>();
        categoryPageResult.setRows(page1.getRecords());
        categoryPageResult.setTotal(page1.getTotal());
        return new Result(true,StatusCode.OK,"",categoryPageResult);
    }

    @PostMapping("/search/{page}/{size}")
    public Result findPage(

            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size,
            @RequestBody Category searchMap
//            @RequestParam(required = false) String name,
//            @RequestParam(required = false) String isShow,
//            @RequestParam(required = false) String isMenu,
//            @RequestParam(required = false) Integer seq,
//            @RequestParam(required = false) Integer parentId
    ) {
        return categoryService.findPage(page, size,searchMap);
    }

//    @GetMapping("/search/{page}/{size}")
//    public Result findPage(
//
//            @PathVariable("page") Integer page,
//            @PathVariable("size") Integer size,
//            @RequestBody Category searchMap
//    ) {
//        return categoryService.findPage(page, size,searchMap);
//    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        return categoryService.findById(id);
    }

    @PutMapping("/{id}")
    public Result updateById(@RequestBody Category category, @PathVariable("id") Integer id) {
        LambdaUpdateWrapper<Category> updateWrapper = Wrappers.lambdaUpdate(Category.class);
        updateWrapper.eq(Category::getId, id);
//                .set(Category::setIsMenu,category.getIsMenu()).

        boolean b = categoryService.update(category, updateWrapper);
        if (!b) {
            return new Result(false, StatusCode.ERROR, "更新失败", "");
        }
        return new Result(true, StatusCode.OK, "更新成功", "");
    }

    @DeleteMapping("/{id}")
    public Result deleteCategory(@PathVariable("id") Integer id) {
        boolean b = categoryService.removeById(id);
        if (!b) {
            return new Result(false, StatusCode.ERROR, "删除失败", "");
        }
        return new Result(true, StatusCode.OK, "删除成功", "");
    }
}

